package com.huanglj.taobaounion.ui.activity;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.huanglj.taobaounion.R;
import com.huanglj.taobaounion.base.BaseActivity;
import com.huanglj.taobaounion.model.domain.TicketResult;
import com.huanglj.taobaounion.presenter.ITicketPresenter;
import com.huanglj.taobaounion.ui.adapter.SelectedPageLeftAdapter;
import com.huanglj.taobaounion.utils.LogUtil;
import com.huanglj.taobaounion.utils.PresenterManager;
import com.huanglj.taobaounion.utils.UrlUtils;
import com.huanglj.taobaounion.view.ITicketCallBack;
import com.huanglj.taobaounion.view.customView.LoadingView;

import butterknife.BindView;

public class TicketActivity extends BaseActivity implements ITicketCallBack {

    private static final String TAG = "TicketActivity";
    boolean mHasTaoBaoApp = false;

    @BindView(R.id.ticket_back)
    ImageView mBackImg;
    @BindView(R.id.ticket_cover_img)
    ImageView mCoverImg;
    @BindView(R.id.ticket_code)
    TextView mCodeText;
    @BindView(R.id.ticket_btn)
    TextView mTicketBtn;
    @BindView(R.id.ticket_loading_img)
    LoadingView mLoadingView;

    private ITicketPresenter mTicketPresenter;
    private String mModel = null;
    private ClipboardManager mCm;
    private String mTicketModel;
    private PackageManager mPm;

    @Override
    protected void initView() {
        mTicketPresenter = PresenterManager.getInstance().getTicketPresenter();
        mTicketPresenter.registerViewCallBack(this);
        mCm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);

    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_ticket;
    }

    @Override
    public void onTicketLoaded(String cover, TicketResult ticketResult) {
        mModel = ticketResult.getData().getTbk_tpwd_create_response().getData().getModel();
        String[] split = mModel.split("\\￥");
        mTicketModel = "￥" + split[1] + "￥";
        LogUtil.d(TAG, mTicketModel);
        mCodeText.setText(mTicketModel);
        Glide.with(this).load(UrlUtils.getCoverPath(cover,800)).into(mCoverImg);
        mLoadingView.setVisibility(View.GONE);
        mLoadingView.setLooper(false);
        mLoadingView.stopLooper();
    }

    @Override
    public void onLoading() {

    }

    @Override
    protected void initEvent() {
        mBackImg.setOnClickListener(v -> {
            finish();
        });
        mTicketBtn.setOnClickListener(v ->{
            ClipData clipData = ClipData.newPlainText("label",mTicketModel);
            mCm.setPrimaryClip(clipData);
            if(mHasTaoBaoApp){

                Intent intent = mPm.getLaunchIntentForPackage("com.taobao.taobao");
//                Intent intent = new Intent();
//                intent.setAction("android.intent.action.MAIN");
//                intent.addCategory("android.intent.category.LAUNCHER");
                startActivity(intent);
            }else {
                Toast.makeText(this,"淘口令复制成功",Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void initPresenter() {
        //act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.taobao.taobao/com.taobao.tao.welcome.Welcome bnds=[589,985][757,1153] (has extras)
        //TODO:判断是否安装了淘宝应用
        mPm = getPackageManager();
        try {
            mPm.getPackageInfo("com.taobao.taobao",PackageManager.MATCH_UNINSTALLED_PACKAGES);
            mHasTaoBaoApp = true;
        } catch (PackageManager.NameNotFoundException e) {
            mHasTaoBaoApp = false;
            e.printStackTrace();
        }
        LogUtil.d(TAG,"mHasTaoBaoApp------->" + mHasTaoBaoApp);
    }

    @Override
    public void onEmpty() {

    }

    @Override
    public void onError(String errorMsg) {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mTicketPresenter.unregisterViewCallBack(this);
    }
}
